package TestPaperSystem.Dto;

import TestPaperSystem.entity.ExamConfig;
import TestPaperSystem.entity.ExamRepo;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author xwj
 * @since 2023-03-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ExamDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    private String id;

    private String examTitle;

    private String examContent;

    private Boolean timeLimit;

    @JsonFormat(shape = JsonFormat.Shape.STRING , pattern = "yyyy-MM-dd HH:mm:ss" , timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startTime;

    @JsonFormat(shape = JsonFormat.Shape.STRING , pattern = "yyyy-MM-dd HH:mm:ss" , timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endTime;

    @JsonFormat(shape = JsonFormat.Shape.STRING , pattern = "yyyy-MM-dd HH:mm:ss" , timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;

    @JsonFormat(shape = JsonFormat.Shape.STRING , pattern = "yyyy-MM-dd HH:mm:ss" , timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    private Integer totalScore;

    private Integer totalTime;

    private Integer qualifyScore;

    private Integer state;

    private Integer openType;

    private String userId;

    private List<ExamRepo> repoList = new ArrayList<>();

    private ExamConfig examConfig;

    public Integer getState() {
        //0:正常 3：已过考试时间 2：未到考试时间
        if (this.timeLimit != null && this.timeLimit) {


            if (System.currentTimeMillis() < startTime.getTime()) {
                return 2;
            }

            if (System.currentTimeMillis() > endTime.getTime()) {
                return 3;
            }

            if (System.currentTimeMillis() > startTime.getTime()
                    && System.currentTimeMillis() < endTime.getTime()
                    && (this.state != 1)) {
                return 0;
            }

        }
        return this.state;
    }
}
